package fr.imag.ufrima.ima.project.ui.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import fr.imag.ufrima.ima.project.ui.main.ImageProject;

public class Menu implements ActionListener {
	private ImageProject imageProject;

	private JMenuItem openImage;
	private JMenuItem saveImage;
	private JMenuItem saveImageAs;
	private JMenuItem exit;
	private JMenuItem annuler, valider;

	public Menu(ImageProject imageProject) {
		this.imageProject = imageProject;

		JMenuBar bar = new JMenuBar(); // create menu bar
		JMenu addMenu = new JMenu("Fichier"); // create Add menu

		openImage = new JMenuItem("Ouvrir image...");
		saveImage = new JMenuItem("Enregistrer");
		saveImageAs = new JMenuItem("Enregistrer sous...");
		exit = new JMenuItem("Quitter");

		addMenu.add(openImage); // add new frame item to Add menu
		addMenu.add(saveImage);
		addMenu.add(saveImageAs);
		addMenu.addSeparator();
		addMenu.add(exit);

		// set up listeners
		openImage.addActionListener(this);
		saveImage.addActionListener(this);
		saveImageAs.addActionListener(this);
		exit.addActionListener(this);

		bar.add(addMenu); // add Add menu to menu bar
		
		addMenu = new JMenu("Edition");

		annuler = new JMenuItem("Annuler");
		valider = new JMenuItem("Valider");

		addMenu.add(annuler);
		addMenu.add(valider);
		
		annuler.addActionListener(this);
		valider.addActionListener(this);

		bar.add(addMenu);
		
		imageProject.setJMenuBar(bar); // set menu bar for this application

		this.hideMenuOptionsForAvailableImages();
	}

	public void showMenuOptionsForAvailableImages() {
		saveImage.setEnabled(true);
		saveImageAs.setEnabled(true);
	}

	public void hideMenuOptionsForAvailableImages() {
		saveImage.setEnabled(false);
		saveImageAs.setEnabled(false);
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		// Handle open button action.
		if (e.getSource() == openImage) {
			imageProject.openImage();
		} else if (e.getSource() == saveImage) {
			imageProject.saveImage();
		} else if (e.getSource() == saveImageAs) {
			imageProject.saveImageAs();
		} else if (e.getSource() == exit) {
			imageProject.exit();
		} else if (e.getSource() == annuler) {
			imageProject.annuler();
			imageProject.getCurrentImageDocument().repaint();
		} else if (e.getSource() == valider) {
			imageProject.valider();
		}
	}

}
